//
//  CYPopupAdView.m
//  cyjf
//
//  Created by tauCross on 2017/8/30.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYPopupAdView.h"

#import "CYActionManager.h"
#import "CYMainController.h"



@interface CYPopupAdView ()

@property(nonatomic, strong)CYPopupAdvertisementObject *object;

@property(nonatomic, strong)UIView *maskView;
@property(nonatomic, strong)UIImageView *adImageView;
@property(nonatomic, strong)UIButton *adButton;
@property(nonatomic, strong)UIButton *ignoreButton;

@end



@implementation CYPopupAdView

- (instancetype)initWithObject:(CYPopupAdvertisementObject *)object
{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if(self)
    {
        self.object = object;
        
        self.maskView = [[UIView alloc] initWithFrame:self.bounds];
        self.maskView.alpha = 0;
        self.maskView.backgroundColor = HEXCOLORA(@"000000", 0.5);
        [self addSubview:self.maskView];
        
        self.adImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 295, 380)];
        self.adImageView.centerX = self.width / 2;
        self.adImageView.bottom = 0;
        self.adImageView.layer.masksToBounds = YES;
        self.adImageView.layer.cornerRadius = 4;
        self.adImageView.image = self.object.image;
        [self addSubview:self.adImageView];
        
        self.adButton = [[UIButton alloc] initWithFrame:self.adImageView.frame];
        [self.adButton addTarget:self action:@selector(onAdButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.adButton];
        
        self.ignoreButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        self.ignoreButton.centerX = self.width / 2;
        self.ignoreButton.top = self.height;
        [self.ignoreButton setImage:CYImage(@"popup_ad_close") forState:UIControlStateNormal];
        [self.ignoreButton addTarget:self action:@selector(onIgnoreButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.ignoreButton];
    }
    return self;
}

#pragma mark - Action
- (void)show
{
    [CYStandardPopupAdvertisementManager adShowedWithadID:self.object.model.id];
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    APP_LOCK_USER_INTERACTION
    [UIView animateWithDuration:0.25
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.maskView.alpha = 1;
                         self.adImageView.centerY = self.height / 2 - 28.5;
                         self.adButton.frame = self.adImageView.frame;
                         self.ignoreButton.top = self.adImageView.bottom + 21;
                     }
                     completion:^(BOOL finished) {
                         APP_UNLOCK_USER_INTERACTION
                     }];
}

- (void)close
{
    APP_LOCK_USER_INTERACTION
    [UIView animateWithDuration:0.25
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.maskView.alpha = 0;
                         self.adImageView.bottom = 0;
                         self.adButton.frame = self.adImageView.frame;
                         self.ignoreButton.top = self.height;
                     }
                     completion:^(BOOL finished) {
                         [self removeFromSuperview];
                         APP_UNLOCK_USER_INTERACTION
                     }];
}

#pragma mark - Control Events
- (void)onIgnoreButtonTouchUpInside
{
    [MobClick event:UM_EVENT_0002];
    [self close];
}

- (void)onAdButtonTouchUpInside
{
    [MobClick event:UM_EVENT_0003];
    [CYStandardActionManager executeActionWithModel:self.object.model.action fromViewController:[CYMainController standardController]];
    [self close];
}

@end
